using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Audio;

namespace TrovaIntruso.core
{
    public static class StaticOperation
    {
        public static Vector2 screen;

        public static string BaseIconDir = "Icone";
        public static Dictionary<string, object> StaticResources { get; set; }
        public static Dictionary<string, Texture2D> Icons { get; set; }
       public static Dictionary<string, SoundEffect> SoundList=new Dictionary<string,SoundEffect>();

        public static SpriteFont DefaultFont { get; set; }

        public static Random rand = new Random(Guid.NewGuid().GetHashCode());
        public static bool AudioEnabled { get; set; }
        private static SoundEffectInstance inst = null;
        public static bool GestureReady { get; set; }
        private static GestureSample _lastg;
        public static GestureSample LastGesture {
            get { return _lastg;}
            set{
                GestureReady = true;
                _lastg = value;
            }
        }

        public static SpriteBatch Batch { get; set; }

        public static Color DefaultColor { get; set; }
        public static Color DefaultColor1 { get; set; }

        public static string HelpText { get; private set; }
        public static string RingrText { get; private set; }

        public static List<Rectangle> GetScreenDivision(int ncomp, Vector2 dim, Vector2 margin)
        {
            int end = (int)(screen.X - (dim.X / 2));
            int start = end - (int)dim.X;

            int slot = (int)(screen.Y - margin.Y) / ncomp;
            int strslot = (int)margin.Y;
            List<Rectangle> rlist = new List<Rectangle>();
            
            for (int i = 0; i < ncomp; i++)
            {
                int last =(int)(margin.Y + (slot - (dim.Y / 2)));
                int str = (int)(last - (dim.Y / 2));

                rlist.Add(new Rectangle(start, str, (int)dim.X, (int)dim.Y));

                margin.Y = last;
            }
            return rlist;

        }

        public static void loadStaticResources(ContentManager manager) {

            StaticResources = new Dictionary<string, object>();
            Icons = new Dictionary<string, Texture2D>();

            DefaultColor = new Color(255, 191, 67);
            DefaultColor1 = Color.White;
            AudioEnabled = true;

            StaticResources.Add("popupback",manager.Load<Texture2D>("popupback"));
            StaticResources.Add("newgame", manager.Load<Texture2D>("newgame"));
            StaticResources.Add("continue", manager.Load<Texture2D>("continue"));
            StaticResources.Add("help", manager.Load<Texture2D>("aiuto"));
            StaticResources.Add("ringraziamenti", manager.Load<Texture2D>("ringr"));
            StaticResources.Add("opzioni", manager.Load<Texture2D>("opzioni"));
            StaticResources.Add("backg", manager.Load<Texture2D>("backgame"));

            StaticResources.Add("facile",manager.Load<Texture2D>("facile"));
            StaticResources.Add("medio",manager.Load<Texture2D>("medio"));
            StaticResources.Add("difficile",manager.Load<Texture2D>("difficile"));

            SoundList.Add("woosh",manager.Load<SoundEffect>("Woosh"));
            SoundList.Add("fail", manager.Load<SoundEffect>("gong"));
            SoundList.Add("click", manager.Load<SoundEffect>("blop"));
            SoundList.Add("success", manager.Load<SoundEffect>("bell"));

            StaticResources.Add("icons", loadIconsSet(manager));
            
            Icons.Add("close", manager.Load<Texture2D>("close"));
            Icons.Add("timebar", manager.Load<Texture2D>("timebar"));
            Icons.Add("volume", manager.Load<Texture2D>("volume"));
            Icons.Add("mute", manager.Load<Texture2D>("mute"));

            DefaultFont = manager.Load<SpriteFont>("Font");

            string[] values = manager.Load<string[]>("StringValues");
            RingrText = values[0];
            HelpText = values[1];
        }

        private static List<IconSet> loadIconsSet(ContentManager manager) {
            string[] temp = { "ball", "car", "cibo", "daft", "food", "gatti", "hallo", "musica", "japan", "sn", "vac", "super" };
            List<IconSet> icons = new List<IconSet>(); 
            foreach (string s in temp) {
                icons.Add(new IconSet(s, manager));
            }

            return icons;

        }

        public static void PlayEffect(string name)
        {
            if (AudioEnabled)
            {
                if (inst != null && inst.State == SoundState.Stopped)
                {
                    inst = SoundList[name].CreateInstance();
                    inst.Play();
                }
                else if (inst == null)
                    inst = SoundList[name].CreateInstance();
            }
        }

        public static void Stop() {
            if (inst != null)
                inst.Stop();
        }

        public static List<Icon> GetRandomSet() {
            List<IconSet> tmp=(List<IconSet>)StaticResources["icons"];
            //Prendo un set di 5 icone esatte
            int set = rand.Next(0, tmp.Count);
            List<Icon> exact = tmp[set].GetExactIcon();

            //Get extra icon
            int ext = rand.Next(0, tmp.Count);
            while (set == ext) ext = rand.Next(0, tmp.Count);
            Icon err = tmp[ext].SpriteList[rand.Next(0, tmp[ext].SpriteList.Count)];
            err.InSet = false;
            exact.Insert(rand.Next(0, 6),err);
            return exact;

        }

        public static Rectangle GetCenteredRectangle(int screenPercentage) {

            return GetCenteredRectangle((int)(screen.X * screenPercentage / 100),(int)(screen.Y * screenPercentage / 100));
        
        }

        public static Rectangle GetCenteredRectangle(int width, int height) {

            int marginw = (int)(screen.X - width) / 2;
            int marginh = (int)(screen.Y - height) / 2;

            return new Rectangle(marginw, marginh, width, height);
        }

        public static string WrapText( string text, float maxLineWidth)
        {
            SpriteFont spriteFont = DefaultFont;
            string[] words = text.Split(' ');

            StringBuilder sb = new StringBuilder();

            float lineWidth = 0f;

            float spaceWidth = spriteFont.MeasureString(" ").X;

            foreach (string word in words)
            {
                Vector2 size = spriteFont.MeasureString(word);

                if (lineWidth + size.X < maxLineWidth)
                {
                    sb.Append(word + " ");
                    lineWidth += size.X + spaceWidth;
                }
                else
                {
                    sb.Append("\n" + word + " ");
                    lineWidth = size.X + spaceWidth;
                }
            }

            return sb.ToString();
        }

        public static bool CanExit { get; set; }
    }
}
